"""
人脸检测
1.人脸检测
2.特征提取
3.相似度的计算
"""
from face_detect import FaceDetect
from face_feat import FaceFeat
from yolo_face_detect import YOLO_face
import cv2
class FaceRec:
    def __init__(self):
        #人脸检测
        self.face_detect = FaceDetect()
        #特征处理
        self.face_feat = FaceFeat()
        self.yolo_face = YOLO_face('weights/yolo_face.onnx')
        pass

    def recognition(self, frame):

        # 人脸检测
        face_img = self.face_detect.detect(frame)
        if face_img is None:
            print("Unrecognized!Please switch angles!")
            return None
        # 特征处理
        face_feat = self.face_feat.get_face_feat(face_img)
        #相似度的计算
        pred_name = self.face_feat.cal_similarity(face_feat)
        det_bboxes, det_conf, det_classid, landmarks = self.yolo_face.detect(frame)
        self.name, self.similarity = pred_name[0],int(round(pred_name[1],2)*100)
        res = self.name+' '+str(self.similarity)
        for box in det_bboxes:
            x, y, w, h = box.astype('int')
            cv2.putText(frame,str(res+'%'),org=(x-2,y-2),fontFace=cv2.FONT_ITALIC,
                        fontScale=1.5,color=(0,0,255),thickness=1)
        return self.name,self.similarity

# if __name__ == '__main__':
#     face = FaceRec()
#     lkw = cv2.imread('users/dll.jpg')
#     face.recognition(lkw)